import os

from flask import Flask, request, session, render_template, url_for
from werkzeug.utils import redirect

from Base.BaseSetting import USERNAME, PASSWORD, Report_dir

app = Flask(__name__)
app.secret_key = 'aaaaasssssoooommmm'

@app.route('/',methods = ['GET','POST'])
def index():
    username = session.get('username')
    if username == USERNAME:
        report_data_list_filename = os.listdir(Report_dir)
        report_name = report_data_list_filename[-1]
        return app.send_static_file('Report/'+report_name)
    else:
        return redirect(url_for('login'))

# @app.route('/pastreport',methods = ['GET','POST'])
# def Past_Report():
#     username = session.get('username')
#     if username == USERNAME:
#         batchinfo = input('请输入批次号：')
#         reports = PastReport().read_pastreport()
#         for report_name in reports:
#             if batchinfo == report_name[0:12]:
#                 return app.send_static_file('Report/' + report_name)
#     else:
#         return redirect(url_for('login'))

@app.route('/login',methods =['GET','POST'])
def login():
    if request.method == 'GET':
        return render_template('index/login.html')
    else:
        #POST 说明提交用户名和密码
        # 接受用户传过来的用户名和密码
        uesername = request.form.get('username')
        #判断数据库有没有该用户
        if uesername == USERNAME:
            # 有用户，判断密码是否正确
            password = request.form.get('password')
            # 提示登陆成功返回首页
            if password == PASSWORD:
                session['username'] = uesername
                #返回一个跳转页面，跳转到index
                return redirect(url_for('index'))

            else:
                return render_template('index/login.html',errmsg = '密码错误')

        # 没有查到用户，提示用户不存在
        else:
            return render_template('index/login.html', errmsg='用户不存在')

if __name__ == '__main__':
    # get_data()
    app.run(debug=True)
